<?php
/**
 * Customizing Elements
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy
 * @license #licence#
 * 
 */
use Fox\Behaviors;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Nawa\Messages;
use Fox\Grid;
use Fox\Html\Block;

$xview = Xview::instance();
$params = is_array($params) ? $params : array();
$msid = $params['msid'];

$gridName = 'followup';
$grid = new Grid($gridName,Generator::url('medicalstudies','followup',null,$params));
$grid->postContentOnReload(true);
$grid->fieldcat(array(
		'msid',
		'itemid',
		'followupid',
		'type',
		'ms_fl_type',
		array('ms_fl_label','code'),
		'position'
));
$grid->fieldcat()->setFieldAttr(array('msid','itemid','followupid','type','code','position'),'SetHiddenInput',true);
$grid->fieldcat()->setFieldAttr(array('msid','itemid','followupid','type','position'),'SetHidden',true);
$grid->fieldcat()->field('position')->classGenerator(Generator::useValue('position'));
$grid->fieldcat()->field('ms_fl_type')->classGenerator(Generator::useValue('cursor-drag'));
$grid->setCoche();
// Follow Items (load object for lable and type)
$followupLabels = array();
$followupItems = Fox\Dao::finder(Nawa\MedicalStudy\Followup\Item::className())
					->where('language = ? AND active = ?',array($xview->language(),1))
					->find();
foreach($followupItems as $item) {
	$followupLabels[$item->followupid] = $item->label;
}		

$grid->fieldcat()->setGenerators(array(
	'ms_fl_type' => Generator::useFunction(function($data) use ($followupLabels) {
		if(isset($followupLabels[$data['followupid']])) {
			return $followupLabels[$data['followupid']];
		} else {
			return null;
		}
	})
));
$grid->fieldcat()->field('code')
		->rules(new \Fox\Grid\Rule('type','==',2))
		->input(true);
$grid->setInput(true,true,false,false,true);
$grid->footer()
		->add(new Button('adnew',Generator::display('addFollowup',
		Generator::url('medicalstudies','followup','add',array('msid' => $msid)),
											'MC_'.$gridName)));
		
$grid->footer()->item(Grid::BUTTON_DELETE_LINE)
			->label(Msg(Fox\Msg::BUNDLE,Fox\Msg::BUT_DELETE));

return $grid;